//
//  BottomDoubleView.swift
//  GomroClient
//
//  Created by mac on 2019/12/12.
//  Copyright © 2019 cn.gomro.app. All rights reserved.
//

import UIKit

@objc protocol BottomDoubleViewDelegate {
    @objc
    func bottomDoubleViewLeftButtonClickAction(sender:UIButton,superView:UIView)
    
    @objc
    func bottomDoubleViewRightButtonClickAction(sender:UIButton,superView:UIView)
}

/** 自定义弹框 */
class BottomDoubleView: UIView {

    weak var delegate:BottomDoubleViewDelegate?
    
    /** 标题 */
    var strTitle:String? {
        didSet{
            self.labTitle.text = strTitle
        }
    }
    
    var btnLeftTitle:String? {
        didSet{
            self.btnOK.setTitle(btnLeftTitle, for: .normal)
        }
    }
    
    var btnRightTitle:String? {
        didSet{
            self.btnRight.setTitle(btnRightTitle, for: .normal)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        
        self.labTitle.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(42)
            make.top.equalTo(64)
        }
        
    }
    
    
    //MARK: - initView
    private func initView(){
        
        self.addSubview(self.labTitle)
        self.addSubview(self.btnOK)
        self.addSubview(self.btnRight)
        
        setNeedsUpdateConstraints()
    }
    
    
    @IBAction private func btnClickAction(sender:UIButton) {
        print("我被点击了")
        
        unowned let weakSelf = self
        if sender == self.btnOK {
            self.delegate?.bottomDoubleViewLeftButtonClickAction(sender: sender, superView: weakSelf)
        }
        else{
            self.delegate?.bottomDoubleViewRightButtonClickAction(sender: sender, superView: weakSelf)
        }
    }
    
    
    //MARK: - lazy load
    lazy var labTitle:YYLabel = {
        let _t = BaseView.createLable(rect: .zero,
                             text: "该物料是否清晰，是否需要驻场工程师澄清？",
                             textColor: UIColor.init().colorFromHexString(hex:"#333333"),
                             font: UIFont.systemFont(ofSize: 16, weight: .bold),
                             backgroundColor: UIColor.clear)
        
        _t.textVerticalAlignment = .top
        _t.numberOfLines = 2
        _t.lineBreakMode = .byWordWrapping
        _t.textAlignment = .center
        return _t
    }()
    
    private lazy var btnOK:UIButton = {
        let _btn = BaseView.createBtn(rect: CGRect.init(x: 0, y: self.height - 39, width: self.width * 0.5, height: 39),
                             strTitle: "是，并保存",
                             titleColor: UIColor.white,
                             txtFont: UIFont.systemFont(ofSize: 15, weight: .bold),
                             image: nil,
                             backgroundColor: nil,
                             borderColor: nil,
                             cornerRadius: 0,
                             isRadius: false,
                             backgroundImage: nil,
                             borderWidth: 0)
        
        let layer = _btn.setCornerRadiusFor(UIRectCorner: [.bottomLeft],
                                            borderWidth: 0,
                                            fillColor:K_APP_TINT_COLOR,
                                            andCorneradius: 5)
        _btn.layer.insertSublayer(layer, at: 0)
        _btn.addTarget(self, action: #selector(btnClickAction(sender:)), for: .touchUpInside)
        
        return _btn
    }()
    
    private lazy var btnRight:UIButton = {
        let _btn = BaseView.createBtn(rect: CGRect.init(x: self.width * 0.5, y: self.height - 39, width: self.width * 0.5, height: 39),
                             strTitle: "否，确认提交",
                             titleColor: UIColor.white,
                             txtFont: UIFont.systemFont(ofSize: 15, weight: .bold),
                             image: nil,
                             backgroundColor: nil,
                             borderColor: nil,
                             cornerRadius: 0,
                             isRadius: false,
                             backgroundImage: nil,
                             borderWidth: 0)
        
        let layer = _btn.setCornerRadiusFor(UIRectCorner: [.bottomRight],
                                            borderWidth: 0,
                                            fillColor:UIColor.init().colorFromHexString(hex:"#C5C9D3"),
                                            andCorneradius: 5)
        _btn.layer.insertSublayer(layer, at: 0)
        _btn.addTarget(self, action: #selector(btnClickAction(sender:)), for: .touchUpInside)
        
        return _btn
    }()
}
